package com.jack.bookshop.servlet;

import com.jack.bookshop.dao.UserDao;
import com.jack.bookshop.vo.ResultVo;
import com.jack.bookshop.vo.UserVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/user-changepw.do")
public class UserChangepwServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置编码格式，避免post提交表单的中文乱码问题
		request.setCharacterEncoding("utf-8");
		// 清空session中的userVo属性
		request.getSession().removeAttribute("userVo");
		String username = request.getParameter("username");
		String oldpassword = request.getParameter("old password");
		String newpassword = request.getParameter("new password");
		// 验证用户输入
		ResultVo validateResultVo = this.validate(username,oldpassword,newpassword);
		if (validateResultVo.getCode() == 1) {
			// 调用dao验证用户名和密码，String.trim() 去除字符串前后的空格
			UserVo userVo = new UserDao().signin(username.trim(), oldpassword.trim());
			if (userVo != null && userVo.getId() > 0) {
				// 用户名密码正确，设置session中的userVo属性
				new UserDao().changepw(username.trim(),newpassword.trim());
				request.getSession().setAttribute("userVo", userVo);
			} else {
				//用户名密码错误，设置request中的resultVo属性
				ResultVo resultVo = new ResultVo();
				resultVo.fail();
				List<String> list = new ArrayList<String>();
				list.add("The username or password may be wrong,failed to change the password,Please try again!");
				resultVo.setList(list);
				request.setAttribute("resultVo", resultVo);
				System.out.println(System.currentTimeMillis() + "\t" + username + "\t Sign In Fail.");
			}
		} else {
			request.setAttribute("resultVo", validateResultVo);
		}
		// 转发到结果页
		request.getRequestDispatcher("changepw-result.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// 验证用户输入
	protected ResultVo validate(String username, String oldpassword,String newpassword) {
		ResultVo resultVo = new ResultVo();
		List<String> list = new ArrayList<String>();

		if (username != null && username.length() <= 20 && username.length() >= 4) {
			// 通过
		} else {
			String s = "Username is too short or too long, it must be 4-20 characters long!";
			list.add(s);
		}
		if (oldpassword != null && oldpassword.length() <= 20 && oldpassword.length() >= 4) {
			// 通过
		} else {
			String s = "password is too short or too long, it must be 4-20 characters long!";
			list.add(s);
		}
		if (newpassword != null && newpassword.length() <= 20 && newpassword.length() >= 4) {
			// 通过
		} else {
			String s = "password is too short or too long, it must be 4-20 characters long!";
			list.add(s);
		}

		if (list.size() > 0) {
			resultVo.fail();
		} else {
			resultVo.success();
		}
		resultVo.setList(list);
		return resultVo;
	}
}
